﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CourseArraging.Common;
using Stone.Library.Common;
using Stone.DataContracts; 

namespace CourseArraging.Frm.jygl
{
    /// <summary>
    /// 版权所有
    /// 石世特@温州医学院 sst330381@gmail.com
    /// </summary> 
    public partial class FrmTecInfo : FrmToolbarBase
    {
        #region 字段属性
       
        //对象字段
        private ModeEntity modeEntity=new ModeEntity(); //默认true表示添加,false表示更新
        private BasicService.BasicServiceClient otherdao = new BasicService.BasicServiceClient();
        protected static FrmTecInfo instance;
        protected YhxxService.YhxxServiceClient yhxxdao =new YhxxService.YhxxServiceClient();

        //事件字段
        public delegate void DelegateSearch();
        public event DelegateSearch SearchOK;

        //数据存储字段
        List<CourseEntity> celist = new List<CourseEntity>();//课程文本框中的已选课程id
        List<GradeEntity> gelist = new List<GradeEntity>();//授课年级
        TecEntity tecentitiy;
        TecArrangeEntity taentity;
        Zap2Entity zap2Entity;
        
        #endregion

        #region 构造器

        public static FrmTecInfo GetInstance()
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new FrmTecInfo();
                instance.MdiParent = GlobalVar.frmMain;
            }
            instance.BringToFront();
            return instance;
        }
        public static FrmTecInfo GetInstance(string tec_id)
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new FrmTecInfo(tec_id);
                instance.MdiParent = GlobalVar.frmMain;
            }
            instance.BringToFront();
            return instance;
        }

        public FrmTecInfo()
        {
            InitializeComponent();
        }
        public FrmTecInfo(string tec_id)
        {
            InitializeComponent();
            modeEntity.changeMode(tec_id);
            AssignData(tec_id);
        }
        #endregion

        #region 事件

        //填写姓名
        private void txt_name_TextChanged(object sender, EventArgs e)
        {
            try
            {
                txt_spell.Text = String2pyHelper.GetPYStr(txt_name.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "出现了未知的错误！");
                return;
            }
        }

        #region 按钮动态效果
        //private void pic_add_Mouse_down(object sender, MouseEventArgs e)
        //{
        //    this.pic_add.Left += 1;
        //    this.pic_add.Top += 1;
        //}
        //private void pic_add_Mouse_up(object sender, MouseEventArgs e)
        //{
        //    this.pic_add.Left -= 1;
        //    this.pic_add.Top -= 1;
        //}
        #endregion

        #endregion

        #region 实现抽象方法

        public override void tsb_new_fun()
        {
            initBaseInfo();
        }

        public override void tsb_save_fun()
        {
            try
            {
                if (modeEntity.formMode)
                    yhxxdao.addTecall(tecentitiy, taentity,zap2Entity);
                else
                {
                    yhxxdao.updateTecall(tecentitiy, taentity, zap2Entity);
                    SearchOK();
                }
                MessageBox.Show("保存成功！");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "保存失败");
            }
        }

        public override bool be4save()
        {
            taentity = CollectArrangeData();
            tecentitiy = CollectBasicData();
            if (txt_name.Text == string.Empty)
            {
                MessageBox.Show("请输入教员姓名 !");
                return false;
            }
            if (combo_sex.Text == string.Empty)
            {
                MessageBox.Show("请输入教员性别 !");
                return false;
            }
            if (txt_gddh.Text == string.Empty && txt_phone.Text == string.Empty)
            {
                MessageBox.Show("请输入联系方式 !");
                return false;
            }
            if (celist.Count == 0)
            {
                MessageBox.Show("请输入主要辅导的科目!");
                return false;
            }
            if (gelist.Count == 0)
            {
                MessageBox.Show("请输入可教年级!");
                return false;
            }
            if (uCkcb1.course_time_string.Count == 0)
            {
                MessageBox.Show("请输入课程时间安排信息!");
                return false;
            }
            return true;
        }

        #endregion

        #region 方法
        public void initBaseInfo()
        { 
            //tab1
            txt_identify.Text = string.Empty;
            txt_name.Text = string.Empty;
            txt_phone.Text = string.Empty;
            txt_spell.Text = string.Empty;
            txt_gddh.Text = string.Empty;
            txt_address.Text = string.Empty;
            txt_bz.Text = string.Empty;
            celist.Clear();
            gelist.Clear();
            //course_id_list = null;
            combo_sex.Text = combo_sex.Items[0].ToString();

            //tab2
            foreach (CheckBox cb in groupBox_course.Controls.OfType<CheckBox>())
                cb.Checked = false;
            foreach (CheckBox cb in groupBox_grade.Controls.OfType<CheckBox>())
                cb.Checked = false;

            //tab3
            this.uCkcb1.setCaption("选择教师可以授课的时间");
            this.uCkcb1.initeDataGrid();

        }

        // 需要在AssignArrangeData执行之后执行
        public TecEntity CollectBasicData()
        {
            TecEntity tecEntity = new TecEntity();
            tecEntity.user_id = int.Parse(modeEntity.id);
            tecEntity.user_name = txt_name.Text;
            tecEntity.user_spell = txt_spell.Text;
            tecEntity.user_sex = combo_sex.Text;
            tecEntity.user_dep = ucCombo_dep1.comboBox1.SelectedValue.ToString();
            tecEntity.user_depname = ucCombo_dep1.comboBox1.Text;
            tecEntity.user_address = txt_address.Text;

            tecEntity.user_gddh = txt_gddh.Text;
            tecEntity.user_phone = txt_phone.Text;
            tecEntity.user_identify = txt_identify.Text;
            tecEntity.user_psw = "123456";
            tecEntity.user_right = ucCombo_right1.comboBox1.Text;
            tecEntity.user_regedit = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            tecEntity.user_free_time = CommonHelper.string2string2DB(uCkcb1.course_time_string);

            tecEntity.user_course = CommonHelper.string2string2DB(celist);
            tecEntity.user_grade = CommonHelper.string2string2DB(gelist);
            tecEntity.user_bz = txt_bz.Text;
            tecEntity.user_free_time_simple = CommonHelper.string2string2DB(uCkcb1.course_time_int);

            return tecEntity;
        }

        public TecArrangeEntity CollectArrangeData()
        {
            TecArrangeEntity taEntity = new TecArrangeEntity();
            celist.Clear();
            gelist.Clear();
            foreach (CheckBox cb in groupBox_course.Controls.OfType<CheckBox>())
                if (cb.Checked)
                    celist.Add(new CourseEntity() {
                        course_id = cb.Tag.ToString(), 
                        course_name=cb.Text
                    });
            foreach (CheckBox cb in groupBox_grade.Controls.OfType<CheckBox>())
                if (cb.Checked)
                    gelist.Add(new GradeEntity() {
                        grade_id = cb.Tag.ToString(),
                        grade_name=cb.Text
                    });
            //为TecArrangeEntity赋值
            taEntity.course_info = celist;
            taEntity.grade_info = gelist;
            //为zapEntity赋值
            zap2Entity = uCkcb1.zapEntity;
            zap2Entity.zapsbname = txt_name.Text;
            return taEntity;
        }

        public void AssignData(string id)
        {
            TecEntity tecEntity = yhxxdao.GetTecinfo8id(id);

            txt_name.Text=tecEntity.user_name;
            txt_spell.Text=tecEntity.user_spell;
            combo_sex.Text=tecEntity.user_sex;
            ucCombo_dep1.comboBox1.Text=tecEntity.user_depname;
            txt_address.Text=tecEntity.user_address;

            txt_gddh.Text= tecEntity.user_gddh;
            txt_phone.Text=tecEntity.user_phone;
            txt_identify.Text=tecEntity.user_identify;
            ucCombo_right1.comboBox1.Text=tecEntity.user_right;
            dateTimePicker1.Text=tecEntity.user_regedit;
            txt_bz.Text=tecEntity.user_bz;

            //user_free_time,user_course,user_grade
            //分解字段
            string[] coursename=CommonHelper.GetSplitString(tecEntity.user_course);
            string[] gradename = CommonHelper.GetSplitString(tecEntity.user_grade);
            string[] freetime = CommonHelper.GetSplitString(tecEntity.user_free_time);

            #region 分别填入响应部分
            //课程信息部分
            for(int i=0;i<coursename.Length;i++)
            {
                foreach (CheckBox cb in groupBox_course.Controls.OfType<CheckBox>())
                {
                    if (cb.Text == coursename[i])
                    {
                        cb.Checked = true;
                        celist.Add(new CourseEntity() {
                            course_id = cb.Tag.ToString(),
                            course_name=cb.Text
                        });
                        break;
                    }
                }
            }
            //年级信息部分
            for (int i = 0; i < gradename.Length; i++)
            {
                foreach (CheckBox cb in groupBox_grade.Controls.OfType<CheckBox>())
                {
                    if (cb.Text == gradename[i])
                    {
                        cb.Checked = true;
                        gelist.Add(new GradeEntity() {
                            grade_id = cb.Tag.ToString(),
                            grade_name=cb.Text
                        });
                        break;
                    }
                }
            }
            //时间信息部分
            for (int i = 0; i < freetime.Length; i++)
            {
                string[] tmpstring = freetime[i].Split(new  char[]{'-'});
                int tmp = 1;
                if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("18:00"))
                    tmp = 5;
                else if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("12:00"))
                    tmp = 3;
                uCkcb1.SetCellData(
                    int.Parse(tmpstring[0]), tmp, null, tmpstring[1] + "-" + tmpstring[2]);
            }
            #endregion
        }
        #endregion

    }
}
